package dao;

import entity.Comment;
import util.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommentDao {
    public void addComment(int blogId, int userId, String content) {
        String sql = "INSERT INTO comment (blogId, userId, content) VALUES (?, ?, ?)";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {
            pst.setInt(1, blogId);
            pst.setInt(2, userId);
            pst.setString(3, content);
            pst.executeUpdate();

            String updateSql = "UPDATE blog SET commentCount = commentCount + 1 WHERE id = ?";
            try (PreparedStatement stmt = conn.prepareStatement(updateSql)) {
                stmt.setInt(1, blogId);
                stmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Comment> getCommentsByBlogId(int blogId, String orderBy) {
        List<Comment> comments = new ArrayList<>();
        String sql = "SELECT * FROM comment WHERE blogId = ? ORDER BY " + orderBy + " DESC";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {

            pst.setInt(1, blogId);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                comments.add(mapResultSetToComment(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return comments;
    }

    public void deleteComment(int commentId, int blogId) {
        String sql = "DELETE FROM comment WHERE id = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {
            pst.setInt(1, commentId);
            pst.executeUpdate();

            String updateSql = "UPDATE blog SET commentCount = commentCount - 1 WHERE id = ?";
            try (PreparedStatement stmt = conn.prepareStatement(updateSql)) {
                stmt.setInt(1, blogId);
                stmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public boolean isUserLikedComment(int userId, int commentId) {
        String sql = "SELECT COUNT(*) FROM `like` WHERE userId = ? AND commentId = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {
            pst.setInt(1, userId);
            pst.setInt(2, commentId);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                return rs.getInt(1) > 0; // 如果返回的计数大于0，表示已点赞
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void likeComment(int userId, int commentId) {
        String sql = "INSERT INTO `like` (userId, commentId) VALUES (?, ?)";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {
            pst.setInt(1, userId);
            pst.setInt(2, commentId);
            pst.executeUpdate();
            String updateLikeCountSql = "UPDATE comment SET likeCount = likeCount + 1 WHERE id = ?";
            try (PreparedStatement updateStmt = conn.prepareStatement(updateLikeCountSql)) {
                updateStmt.setInt(1, commentId);
                updateStmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void unlikeComment(int userId, int commentId) {
        String sql = "DELETE FROM `like` WHERE userId = ? AND commentId = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {
            pst.setInt(1, userId);
            pst.setInt(2, commentId);
            pst.executeUpdate();

            String updateLikeCountSql = "UPDATE comment SET likeCount = likeCount - 1 WHERE id = ? AND likeCount > 0";
            try (PreparedStatement updateStmt = conn.prepareStatement(updateLikeCountSql)) {
                updateStmt.setInt(1, commentId);
                updateStmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private Comment mapResultSetToComment(ResultSet rs) throws SQLException {
        return new Comment(
                rs.getInt("id"),
                rs.getInt("blogId"),
                rs.getInt("userId"),
                rs.getString("content"),
                rs.getInt("likeCount"),
                rs.getTimestamp("createdTime")
        );
    }

}
